package com.ljm.boot.tess4j.controller;

import com.ljm.boot.tess4j.enums.LanguageTypeEnum;
import com.ljm.boot.tess4j.model.OcrResult;
import com.ljm.boot.tess4j.server.TesseractServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @Description
 * @Author Dominick Li
 **/
@Slf4j
@RestController
@RequestMapping("/api")
public class OcrController {

    @Resource
    private TesseractServer tesseractServer;

    /**
     * OCR识别  /ocr/chi_sim          /ocr/eng
     * @param language 使用的模型语种  chi_sim=简体中文  eng=英文
     * @param file 需要识别的图片
     */
    @PostMapping("/ocr/{language}")
    public OcrResult recognize(@PathVariable String language, MultipartFile file) {
        try {
            // 对图片进行文字识别
            return tesseractServer.ocrImage(LanguageTypeEnum.getLanguageByType(language), file);
        } catch (Exception e) {
            log.error("error:{}", e.getMessage());
            return OcrResult.fail(e.getMessage());
        }
    }

}
